import { Injectable } from '@angular/core';
import {
  Http,
  Response,
  URLSearchParams,
  Headers,
  RequestOptions,
  RequestOptionsArgs
} from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { environment } from '../../../environments/environment';

@Injectable()
export class ChannelManageService {
    private apiBaseUrl = environment.apiBaseUrl;

    constructor (private http: Http) {}

    // http://47.100.161.202:8080/admin/channel/list
    // 获取一级渠道列表接口
    getChannel(param: object): Observable<object> {
        const params: URLSearchParams = new URLSearchParams();
        params.set('channelName', param['channelName']);
        params.set('pageSize', param['pageSize']);
        params.set('pageNumber', param['pageNumber']);
        return this.http.get(this.apiBaseUrl + 'admin/channel/list', { params: params })
                .map(this.extractData)
                .catch(this.handleError);
    }

    // 获取所有渠道接口
    getChannelTree(): Observable<object> {
        return this.http.get(this.apiBaseUrl + 'admin/channel/getTree')
                .map(this.extractData)
                .catch(this.handleError);
    }

    // 新增渠道接口
    postCreateChannel(param: object): Observable<object> {
        const params: URLSearchParams = new URLSearchParams();
        params.set('name', param['name']);
        params.set('order', param['order']);
        params.set('parentId', param['parentId']);
        params.set('imagePath', param['imagePath']);

        return this.http.post(this.apiBaseUrl + 'admin/channel/save', params)
            .map(res => res.text() ? res.json() : {})
            .catch(this.handleError);
    }
    // 修改渠道接口
    postUpdateChannel(param: object): Observable<object> {
        const params: URLSearchParams = new URLSearchParams();
        params.set('id', param['id']);
        params.set('name', param['name']);
        params.set('order', param['order']);
        params.set('parentId', param['parentId']);
        params.set('imagePath', param['imagePath']);

        return this.http.post(this.apiBaseUrl + 'admin/channel/update', params)
            .map(res => res.text() ? res.json() : {})
            .catch(this.handleError);
    }
    // 删除渠道接口
    postDeleteChannel(arr: any): Observable<object> {
        const data = new URLSearchParams();
        let paramUrl = '';
        arr.forEach((pm, index) => {
            if ((index + 1) === arr.length) {
                paramUrl += pm;
            } else {
                paramUrl += pm + ',';
            }
        });
        data.append('ids', paramUrl);
        return this.http.post(this.apiBaseUrl + 'admin/channel/delete', data)
            .map(res => res.text() ? res.json() : {})
            .catch(this.handleError);
    }

    private extractData(res: Response) {
        const body = res.json();
        return body || { };
    }

    private handleError (error: Response | any) {
        return Observable.throw(error);
    }
}
